package com.tgjorgoski.window;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;

import com.tgjorgoski.repository.HibernatePersistManager;
import com.tgjorgoski.repository.IPersistManager;
import com.tgjorgoski.sidekicknotes.htmlview.HTMLEditor;


public class Session {
	static IPersistManager pManager=null;
	static String userHome = System.getProperty("user.home");
	static SideNotesApp oneApplication;
	
	static List<PropertyChangeListener> globalListeners = new ArrayList<PropertyChangeListener>();
	
	public static String getIndexFolder() {
		return  getHomeFolder() + File.separator + "index";
	}
	public static String getNotesFolder() {
		return getHomeFolder() + File.separator + "notes";
	}
	public static String getHomeFolder() {
		return userHome + File.separator + ".sidenotes";
	}
	public static String getDBFolder() {
		return getHomeFolder() + File.separator + "db";
	}
	
	public static IPersistManager getPersistManager() {
		if (pManager==null) {
			pManager = new HibernatePersistManager();
		}
		return pManager;
	}
	
	public static String PREF_SPELLCHECK = "SPELLCHECK";
	public static String PREF_LOCKED = "APPLOCKED";
	private static Boolean cachedDefaultSpellcheck = null;
	private static Boolean cachedDefaultAppLocked = null;
	
	public static boolean isAppLockedByDefaultOn() {
		if (cachedDefaultAppLocked==null) {
			Preferences pr = Preferences.userNodeForPackage(SideNotesApp.class);
			cachedDefaultAppLocked=pr.getBoolean(PREF_LOCKED, false);
		}
		return cachedDefaultAppLocked;
	}
	public static void setAppLocked(boolean appLocked) {
		boolean oldValue = isAppLockedByDefaultOn();
		if (oldValue==appLocked) return;
		Preferences pr = Preferences.userNodeForPackage(SideNotesApp.class);
		pr.putBoolean(PREF_LOCKED, appLocked);
		cachedDefaultAppLocked=appLocked;
		firePropertyChanged(PREF_LOCKED, oldValue, appLocked);
	}
	public static boolean isSpellcheckByDefaultOn() {
		if (cachedDefaultSpellcheck==null) {
			Preferences pr = Preferences.userNodeForPackage(SideNotesApp.class);
			cachedDefaultSpellcheck=pr.getBoolean(PREF_SPELLCHECK, true);
		}
		return cachedDefaultSpellcheck;
	}
	public static void setSpellecheck(boolean spellcheck) {
		boolean oldValue = isSpellcheckByDefaultOn();
		if (oldValue==spellcheck) return;
		Preferences pr = Preferences.userNodeForPackage(SideNotesApp.class);
		pr.putBoolean(PREF_SPELLCHECK, spellcheck);
		cachedDefaultSpellcheck=spellcheck;
		firePropertyChanged(PREF_SPELLCHECK, oldValue, spellcheck);
	}
	
	public static void addPropertyChangeListener(PropertyChangeListener pcl) {
		globalListeners.add(pcl);
	}
	
	public static void removePropertyChangeListener(PropertyChangeListener pcl) {
		globalListeners.remove(pcl);
	}
	
	public static void firePropertyChanged(String property, Object oldValue, Object newValue) {
		List<PropertyChangeListener> listenersCopy = new ArrayList<PropertyChangeListener>(globalListeners);
		for (PropertyChangeListener listener : listenersCopy) {
			listener.propertyChange(new PropertyChangeEvent(Session.class, property, oldValue, newValue));
		}
	}
	
	public static void setApplication(SideNotesApp application) {
		oneApplication=application;
	}
	
	public static SideNotesApp getApplication() {
		return oneApplication;
	}
	public static HTMLEditor getHTMLEditor() {
		return oneApplication.getView().getEditor();
	}
	public static HTMLView getHTMLView() {
		return oneApplication.getView();
	}
}
